######################################################################
#Replication file for "The Impact of China's AIIB on the World Bank"
#Authors: Jing Qian, James Vreeland, Jianzhi Zhao
#Codes to replicate results in Table C.1
######################################################################
rm(list = ls())

#Other packages used
library(tidyverse)
library(lmtest)
library(sandwich)
library(alpaca)
library(MASS)
library(stargazer)

#Setwd
setwd("C:/Users/qianj/Dropbox (Princeton)/AIIB_WB_Replication")

#Load custom functions
#Note: Given the number of tests performed in the appendix, 
#several custom functions are created to implement estimation methods in a more efficient way.
#These functions merely create a wrap-up of existing functions, like bpCausal and gsynth,
#in order to avoid typing parameters repeatedly.

source("code/custom_functions.R")

###################################
#Produce Table C.1
###################################

#-----------------------
#(1) Load data
#-----------------------
load("data/data_vote.RData")


#-----------------------
#(2) Setup
#-----------------------
#covariates
covs = c("gdppc_log_lag",
         "population_log_lag",
         "election_either_lag",
         "fdi_gdp_lag",
         "debt_gni_lag",
         "oda_gni_lag",
         "polity2_lag",
         "unsc_lag",
         "IdealPointDistance_lag",
         "amount_clean_log")

#FE
fe.use = c("factor(iso2c)", "factor(year)")

#Treatment
treat.use = "aiib_founder_2016"

#Outcome
dv.use = "us_support"

#-----------------------
#(3) Column (1) 
#-----------------------
#Data
df.use = df.vote

#Equation
eq = make.formula(dv.use, treat.use)

#Estimation
tab.c1.1 = feglm.use(eq, df.use)

#Rounding
tab.c1.1$se$cm %>%
  round(digits = 3) %>%
  format(nsmall = 3)

#Number of observations
tab.c1.1$fit$data %>%
  nrow()

#Number of countries
tab.c1.1$fit$data$iso2c %>%
  unique() %>%
  length()

#AIIB founders
tab.c1.1$fit$data %>%
  subset(aiib_founder_2016 == 1) %>%
  .$iso2c %>%
  unique() %>%
  length()

#-----------------------
#(4) Column (2) 
#-----------------------
#Data
df.use = df.vote

#Equation
eq = make.formula(dv.use, treat.use, covs)

#Estimation
tab.c1.2 = feglm.use(eq, df.use)

#Rounding
tab.c1.2$se$cm %>%
  round(digits = 3) %>%
  format(nsmall = 3)

#Number of observations
tab.c1.2$fit$data %>%
  nrow()

#Number of countries
tab.c1.2$fit$data$iso2c %>%
  unique() %>%
  length()

#AIIB founders
tab.c1.2$fit$data %>%
  subset(aiib_founder_2016 == 1) %>%
  .$iso2c %>%
  unique() %>%
  length()

#-----------------------
#(5) Column (3) 
#-----------------------
#Data
df.use = df.vote %>%
  subset(iso2c != "CN")

#Equation
eq = make.formula(dv.use, treat.use)

#Estimation
tab.c1.3 = feglm.use(eq, df.use)

#Rounding
tab.c1.3$se$cm %>%
  round(digits = 3) %>%
  format(nsmall = 3)

#Number of observations
tab.c1.3$fit$data %>%
  nrow()

#Number of countries
tab.c1.3$fit$data$iso2c %>%
  unique() %>%
  length()

#AIIB founders
tab.c1.3$fit$data %>%
  subset(aiib_founder_2016 == 1) %>%
  .$iso2c %>%
  unique() %>%
  length()

#-----------------------
#(6) Column (4) 
#-----------------------
#Data
df.use = df.vote %>%
  subset(iso2c != "CN")

#Equation
eq = make.formula(dv.use, treat.use, covs)

#Estimation
tab.c1.4 = feglm.use(eq, df.use)

#Rounding
tab.c1.4$se$cm %>%
  round(digits = 3) %>%
  format(nsmall = 3)

#Number of observations
tab.c1.4$fit$data %>%
  nrow()

#Number of countries
tab.c1.4$fit$data$iso2c %>%
  unique() %>%
  length()

#AIIB founders
tab.c1.4$fit$data %>%
  subset(aiib_founder_2016 == 1) %>%
  .$iso2c %>%
  unique() %>%
  length()
